package com.scsj.supermarket.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.orhanobut.logger.Logger;
import com.scsj.supermarket.R;
import com.scsj.supermarket.bean.CommAlreadyBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的收藏
 */

public class MineCollectionAdapter extends BaseQuickAdapter<CommAlreadyBean, BaseViewHolder> {

    private LinearLayout itemTopLayout;
    private ImageView lookMoreIv;
    private ImageView lookMoreWhiteIv;
    public CheckBox checkBox;
    private List<CommAlreadyBean> data;
    //我们需要创建一个集合来记录被选中的条目的位置
    private List<Boolean> isSelected;
    //还需要创建一个集合来记录选中的条目的位置
    private List<Boolean> isItemChecked;
    private boolean isEdit;
    private boolean isSelectedAll;
    private int whichPage;
    private Context context;
    private ArrayList<Boolean> isCheckedList;


    public MineCollectionAdapter(List<CommAlreadyBean> data, ArrayList<Boolean> isCheckedList, int whichPage, Context context) {
        super(R.layout.item_mine_collect_layout, data);
        this.isCheckedList = isCheckedList;
        this.whichPage = whichPage;
        this.context = context;
        this.data = data;

        isSelected = new ArrayList<>();
        isItemChecked = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            isSelected.add(false);
        }

    }

    @Override
    protected void convert(final BaseViewHolder helper, CommAlreadyBean item) {

        if (whichPage == 0) {
            //店铺收藏
        } else {
            //商品收藏
        }
        //选择框
        checkBox = helper.getView(R.id.shop_checkbox);
        //查看更多按钮
        lookMoreIv = helper.getView(R.id.collect_dots_iv);
        //顶部遮罩层布局
        itemTopLayout = helper.getView(R.id.item_top_lapout);
        //白色小点点
        lookMoreWhiteIv = helper.getView(R.id.white_dots_iv);

        //判断顶层遮罩布局显示与否
        if (isSelected.get(helper.getLayoutPosition())) {
            itemTopLayout.setVisibility(View.VISIBLE);
            lookMoreIv.setVisibility(View.GONE);
        } else {
            itemTopLayout.setVisibility(View.GONE);
            lookMoreIv.setVisibility(View.VISIBLE);
        }

        //点击灰色小点点，显示顶部遮罩布局
        lookMoreIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < isSelected.size(); i++) {
                    isSelected.set(i, true);
                }
                //  isSelected.set(helper.getLayoutPosition(), true);
                notifyDataSetChanged();
            }
        });

        //点击白色小点点，隐藏所有的顶部遮罩层布局
        lookMoreWhiteIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < isSelected.size(); i++) {
                    isSelected.set(i, true);
                }
                notifyDataSetChanged();
            }
        });

        //处于编辑状态，显示可编辑选择框，否则隐藏
        if (isEdit) {
            helper.getView(R.id.shop_checkbox).setVisibility(View.VISIBLE);
        } else {
            helper.getView(R.id.shop_checkbox).setVisibility(View.GONE);
        }

        //全选按钮是否点击
        if (isSelectedAll) {
            checkBox.setChecked(true);
        } else {
            checkBox.setChecked(false);
        }
    }

    public void setCheced(ArrayList<Boolean> list) {
        isCheckedList = list;
        for (int i = 0; i < list.size(); i++) {
              if (list.get(i)){
                  checkBox.setChecked(true);
              }else{
                  checkBox.setChecked(false);
              }
        }
    }

    //外部调用方法，判断是否处于可编辑状态
    public void setEdit(boolean b) {
        isEdit = b;
        notifyDataSetChanged();
    }

    //外部调用方法，判断是否全选
    public void setSelectAllItem(boolean isSelectedAll) {
        isEdit = true;
        this.isSelectedAll = isSelectedAll;
        notifyDataSetChanged();
    }

    //外部调用方，用来删除所选中的条目
    public void deleteSelectedItem(ArrayList<Boolean> list) {
        for (int i = list.size() - 1; i >= 0; i--) {
            if (list.get(i)) {
                data.remove(i);
                list.remove(i);
                notifyItemRemoved(i);
                notifyItemChanged(i, data.size());
            }
        }

//        for (int i = 0; i < data.size(); i++) {
//            isItemChecked.set(i,false);
//        }
//
//        if (isSelectedAll) {
//            data.clear();
//        }
    }
}
